package com.pet.service.impl;


import com.pet.context.BaseContext;
import com.pet.dto.web.PetHelpInformationDTO;
import com.pet.dto.web.PetHelpRemarkDTO;
import com.pet.entity.PetHelpInformation;
import com.pet.entity.PetHelpRemark;
import com.pet.mapper.PetHelpInformationMapper;
import com.pet.mapper.UserInformationMapper;
import com.pet.service.PetHelpInformationService;
import com.pet.vo.web.PetHelpInformationVO;
import com.pet.vo.web.PetHelpRemarkVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class PetHelpInformationServiceImpl implements PetHelpInformationService {

    @Autowired
    private PetHelpInformationMapper petHelpInformationMapper;
    @Autowired
    private UserInformationMapper userInformationMapper;


    /**
     * 添加求助信息
     * @param dto
     */
    public void insertPetHelpInformation(PetHelpInformationDTO dto) {
        dto.setId(0);
        //创建实体
        PetHelpInformation entity = new PetHelpInformation();
        //赋值
        BeanUtils.copyProperties(dto, entity);

        entity.setCreateTime(LocalDateTime.now());
        entity.setCreateUser(Math.toIntExact(BaseContext.getCurrentId()));
        entity.setUpdateTime(LocalDateTime.now());
        entity.setUpdateUser(Math.toIntExact(BaseContext.getCurrentId()));
        //操作数据库
        petHelpInformationMapper.insertPetHelpInformation(entity);
    }

    /**
     * 查询求助信息 list
     * @return
     */
    public List<PetHelpInformationVO> selectPetHelpInformation() {
        //创建vo list
        List<PetHelpInformationVO> voList = new ArrayList<>();
        //创建实体并获取
        List<PetHelpInformation> entityList = petHelpInformationMapper.selectPetHelpInformation();
        //转换
        for (PetHelpInformation e : entityList) {
            //创建vo
            PetHelpInformationVO vo = new PetHelpInformationVO();
            //赋值vo
            BeanUtils.copyProperties(e, vo);
            vo.setCreateUser(e.getName());
            //对 接种疫苗情况 是否驱虫情况 是否绝育情况进行判断
            if(e.getVaccine()==1){
                vo.setVaccine("已接种");
            }else if(e.getVaccine()==2){
                vo.setVaccine("未接种");
            }else{
                vo.setVaccine("未记录");
            }

            if (e.getInsectRepellent()==1){
                vo.setInsectRepellent("已驱虫");
            } else if (e.getInsectRepellent()==2) {
                vo.setInsectRepellent("未驱虫");
            }else{
                vo.setInsectRepellent("未记录");
            }

            if (e.getSterilization()==1){
                vo.setSterilization("已绝育");
            } else if (e.getSterilization()==2) {
                vo.setSterilization("未绝育");
            }else{
                vo.setSterilization("未记录");
            }
            //查询用户头像字段profile_photo 并添加到vo
            vo.setProfilePhoto(userInformationMapper.selectProfilePhotoById(e.getCreateUser()));
            voList.add(vo);
        }
        
        return voList;
    }

    /**
     * 发布评论
     * @param dto
     */
    public void insertPetHelpRemark(PetHelpRemarkDTO dto) {
        //创建实体
        PetHelpRemark entity = new PetHelpRemark();
        //赋值
        BeanUtils.copyProperties(dto, entity);
        entity.setCreateTime(LocalDateTime.now());
        entity.setCreateUser(Math.toIntExact(BaseContext.getCurrentId()));

        //操作数据库
        log.info("{}",entity);
        petHelpInformationMapper.insertPetHelpRemark(entity);
    }

    /**
     * 查询指定求助帖子的评论
     * @param helpId
     * @return
     */
    public List<PetHelpRemarkVO> selectPetHelpRemark(Integer helpId) {
        //创建vo list
        List<PetHelpRemarkVO> voList = new ArrayList<>();
        //创建实体并获取
        List<PetHelpRemark> entityList = petHelpInformationMapper.selectPetHelpRemark(helpId);
        //转换
        for (PetHelpRemark entity : entityList) {
            //创建vo
            PetHelpRemarkVO vo = new PetHelpRemarkVO();
            //赋值vo
            BeanUtils.copyProperties(entity, vo);
            //获取用户昵称
            vo.setCreateUser(userInformationMapper.selectName(entity.getCreateUser()));

            voList.add(vo);
        }
        log.info("voList:{}",voList);
        return voList;
    }

    /**
     * 删除求助信息
     * @param id
     */
    @Transactional
    public void deletePetHelpInformation(Long id) {
        //删除求助信息
        petHelpInformationMapper.deletePetHelpInformation(id);
        //根据Id(helpId) 删除求助信息对应的评论
        try {
            petHelpInformationMapper.deletePetHelpRemarkByHelpId(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return;
    }

    /**
     * 查询个人求助信息
     * @return
     */
    public List<PetHelpInformationVO> selectPetInfoBySelfId(Integer userId) {
        if (userId==null){
            userId = Math.toIntExact(BaseContext.getCurrentId());
        }
        List<PetHelpInformationVO> voList = new ArrayList<>();

        List<PetHelpInformation> eList = petHelpInformationMapper.selectPetHelpInfosBySelfId(userId);
        String createName = userInformationMapper.selectName(Math.toIntExact(userId));
        //遍历
        for (PetHelpInformation e : eList) {
            PetHelpInformationVO vo = new PetHelpInformationVO();
            BeanUtils.copyProperties(e,vo);
            if(e.getVaccine()==1){
                vo.setVaccine("已接种");
            }else if(e.getVaccine()==2){
                vo.setVaccine("未接种");
            }else{
                vo.setVaccine("未记录");
            }

            if (e.getInsectRepellent()==1){
                vo.setInsectRepellent("已驱虫");
            } else if (e.getInsectRepellent()==2) {
                vo.setInsectRepellent("未驱虫");
            }else{
                vo.setInsectRepellent("未记录");
            }

            if (e.getSterilization()==1){
                vo.setSterilization("已绝育");
            } else if (e.getSterilization()==2) {
                vo.setSterilization("未绝育");
            }else{
                vo.setSterilization("未记录");
            }

            vo.setCreateUser(createName);
            voList.add(vo);
        }


        return voList;
    }

    /**
     * 修改个人帖子
     * @param petHelpInformationDTO
     */
    public void updatePetHelpInformation(PetHelpInformationDTO petHelpInformationDTO) {
        PetHelpInformation petHelpInformation = new PetHelpInformation();
        BeanUtils.copyProperties(petHelpInformationDTO,petHelpInformation);
        //更新信息
        petHelpInformation.setUpdateUser(Math.toIntExact(BaseContext.getCurrentId()));
        petHelpInformation.setUpdateTime(LocalDateTime.now());
        log.info("{}",petHelpInformation);
        petHelpInformationMapper.updatePetHelpInformation(petHelpInformation);
    }



}
